﻿#ifndef CANTHREAD_H
#define CANTHREAD_H

#include <QThread>
#ifdef _WINDOWS
#include <ControlCAN.h>
#else
#include <controlcan.h>
#endif
#include <QDebug>
#include <map>
#include <qqueue.h>

class CANThread :public QThread
{
	Q_OBJECT
public:
	static constexpr size_t CANDATASIZE = 8;
	struct CanData {
		UINT id;
		unsigned char data[CANDATASIZE];
		size_t len;
	};

	CANThread();

	void stop();

	//1.打开设备
	bool openDevice(UINT deviceType, UINT debicIndex, UINT baudRate0, UINT baudRate1);

	//2.初始化CAN
	bool initCAN();

	//3.启动CAN
	bool startCAN();

	//4.发送数据
	bool sendData(UINT channel, UINT ID, BYTE remoteFlag, BYTE externFlag, const unsigned char* data, size_t len);
	bool sendData(UINT channel, UINT ID, const unsigned char* data, size_t len);

	int recvData(UINT channel, QQueue<CanData>& data);
	int recvData(UINT channel, QVector<CanData>& data);

	//5.关闭设备
	void closeDevice();

	//0.复位设备，  复位后回到3
	bool reSetCAN();

	UINT m_deviceType;
	UINT m_deviceIndex;
	UINT m_baudRate0, m_baudRate1;
	UINT m_debicCom;

	bool stopped;

signals:
	void getProtocolData(VCI_CAN_OBJ* vci, unsigned int dwRel, unsigned int channel);
	void boardInfo(VCI_BOARD_INFO vbi);

private:
	void run();
	void sleep(int msec);
};

#endif // CANTHREAD_H
